import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
/*import Mock from './mock'  //注释之后避免mockjs拦截请求
Mock.bootstrap();*/
import 'font-awesome/css/font-awesome.min.css'
//给每个异步请求添加默认前缀
axios.defaults.baseURL = 'http://localhost';



/*//1 使用axios前置拦截器，让所有的请求都携带uToken
axios.interceptors.request.use(config=>{
  //携带token
  let uToken =  localStorage.getItem("token");
  if(uToken){
    config.headers['U-TOKEN']=uToken;
  }
  return config;
},error => {
  Promise.reject(error);
})
//2 使用axios后置拦截器，处理没有登录请求
axios.interceptors.response.use(result=>{
  let data = result.data;
  //如果返回的状态为1 ，说明redis过期，清空前台存储的token和loginUser
  if (data.state === 1){
    localStorage.removeItem("token");
    localStorage.removeItem("user");
  }

  if(!data.success && data.msg==="noUser")
    this.$router.push({ path: '/login' });
  return result;
},error => {
  Promise.reject(error);
})*/









//导入axios，发送Ajax请求
import axios from 'axios';
//给vue添加一个原型
Vue.prototype.$http=axios;

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  //NProgress.start();
  if (to.path == '/login') {
    localStorage.removeItem('user');
  }
  let user = JSON.parse(localStorage.getItem('user'));
  if (!user && to.path != '/login') {
    next({ path: '/login' })
  } else {
    next()
  }
})

//router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

